package com.sbl.bajw.util;

import java.io.BufferedReader;
import java.io.Reader;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class JsonUtil {
	
	static JsonConfig config = new JsonConfig();
	
	static{
		config.registerJsonValueProcessor(Date.class, 
				new JsonValueProcessor() {  
				 
		            private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");  
		            @Override 
		            public Object processObjectValue(String key, Object value, JsonConfig cfg) {  
		            	if(value == null){
		            		return "";
		            	}
		                return format.format((Date)value);  
		            }  
		 
		            @Override 
		            public Object processArrayValue(Object value, JsonConfig cfg) {  
		                return format.format((Date)value);  
		            }  
			});
		
		config.registerJsonValueProcessor(Timestamp.class, 
				new JsonValueProcessor() {  
				 
		            private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		            @Override 
		            public Object processObjectValue(String key, Object value, JsonConfig cfg) {  
		            	if(value == null){
		            		return "";
		            	}
		                return format.format((Timestamp)value);  
		            }  
		 
		            @Override 
		            public Object processArrayValue(Object value, JsonConfig cfg) {  
		                return format.format((Date)value);  
		            }  
			});
		
		config.registerJsonValueProcessor(oracle.sql.CLOB.class, 
				new JsonValueProcessor() {

			  @Override 
	            public Object processObjectValue(String key, Object value, JsonConfig cfg) {  
	            	if(value == null){
	            		return "";
	            	}
	            	String reString = "";
	            	oracle.sql.CLOB clob =  (oracle.sql.CLOB)value;
	            	try {
	            		Reader is = clob.getCharacterStream();// 得到流
		            	BufferedReader br = new BufferedReader(is);
		            	String s = br.readLine();
		            	StringBuffer sb = new StringBuffer();
		            	while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
			            	sb.append(s);
			            	s = br.readLine();
			            	reString = sb.toString();
		            	} 
		            	return reString;
					} catch (Exception e) {
						return "json convert error";
					}
			  }

				@Override
				public Object processArrayValue(Object arg0, JsonConfig arg1) {
					// TODO Auto-generated method stub
					return null;
				}
		           
			});
	}
	
	public static String ConvertArrayToJson(Object obj){
		if(obj == null){
			return "";
		}
		JSONArray jsonObj = JSONArray.fromObject(obj,config);
		String jsonStr = jsonObj.toString();
		return jsonStr;
	}
	
	public static String ConvertObjectToJson(Object obj){
		if(obj == null){
			return "";
		}
		JSONObject jsonObj = JSONObject.fromObject(obj,config);
		String jsonStr = jsonObj.toString();
		return jsonStr;
	}
	
}
